//
//  LoginViewController.m
//  OneCreditTreasure
//
//  Created by 夏克镭 on 16/8/25.
//
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "CarInfoTableViewCell.h"

@interface LoginViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *registerBtn;
@property (nonatomic, strong) UITextField *phoneNumTF;
@property (nonatomic, strong) UITextField *pwdTF;
@end

@implementation LoginViewController
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 100) {
        
        if (_pwdTF) [self.pwdTF becomeFirstResponder];
        
    } else if (textField.tag == 101) {
        [textField resignFirstResponder];
    }
    return YES;
}

#pragma mark - UITableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CarInfoTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    Cell.infoTF.delegate = self;
    
    if (indexPath.section == 0) {
        Cell.leftLB.text = @"手机号码:";
        Cell.infoTF.placeholder = @"请输入您的手机号码";
        Cell.infoTF.returnKeyType = UIReturnKeyNext;
        self.phoneNumTF = Cell.infoTF;
        Cell.infoTF.tag = 100;
    } else {
        Cell.leftLB.text = @"密码:";
        Cell.infoTF.secureTextEntry = YES;
        Cell.infoTF.placeholder = @"请输入密码";
        self.pwdTF = Cell.infoTF;
        Cell.infoTF.tag = 101;
    }
    
    return Cell;
}

#pragma mark - UITableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10 * AUTO_WIDTH;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5 * AUTO_WIDTH;
}

#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kRGBA(234, 234, 234, 1);
    [self tableView];
    [self loginBtn];
    [self registerBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init {
    if (self = [super init]) {
        self.title = @"登陆";
    }
    return self;
}

#pragma mark - 方法 methods
- (void)checkLogin:sender {
    [self.navigationController popViewControllerAnimated:YES];
    [self.delegate checkMyLogin:sender];
}

- (void)goToRegisterPage:sender {
    RegisterViewController *vc = [RegisterViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载 lazy loading
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(0);
            make.height.equalTo(250);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        
        [_tableView registerClass:[CarInfoTableViewCell class] forCellReuseIdentifier:@"Cell"];
    }
    return _tableView;
}

- (UIButton *)loginBtn {
    if(_loginBtn == nil) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.view addSubview:_loginBtn];
        [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(80);
            make.right.equalTo(-80);
            make.top.equalTo(self.tableView.mas_bottom).equalTo(0);
            make.height.equalTo(40);
        }];
        
        [_loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
        [_loginBtn setTintColor:[UIColor redColor]];
        [_loginBtn addTarget:self action:@selector(checkLogin:) forControlEvents:UIControlEventTouchUpInside];
        [_loginBtn setBackgroundColor:[UIColor whiteColor]];
        
        _loginBtn.layer.cornerRadius = 8;
        _loginBtn.layer.borderWidth = 1;
        _loginBtn.layer.borderColor = [[UIColor redColor] CGColor];
    }
    return _loginBtn;
}

- (UIButton *)registerBtn {
    if(_registerBtn == nil) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.view addSubview:_registerBtn];
        [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(80);
            make.right.equalTo(-80);
            make.height.equalTo(40);
            make.top.equalTo(self.loginBtn.mas_bottom).equalTo(15);
        }];
        
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_registerBtn setTintColor:[UIColor redColor]];
        [_registerBtn addTarget:self action:@selector(goToRegisterPage:) forControlEvents:UIControlEventTouchUpInside];
        [_registerBtn setBackgroundColor:[UIColor whiteColor]];
        
        _registerBtn.layer.cornerRadius = 8;
        _registerBtn.layer.borderWidth = 1;
        _registerBtn.layer.borderColor = [[UIColor redColor] CGColor];
    }
    return _registerBtn;
}

@end
